﻿package com.bn.NNYY;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainView extends SurfaceView 
implements SurfaceHolder.Callback{
	
	MyActivity activity;
	Paint paint;//画笔
	
	KeyThread keythread;//主界面的刷帧线程
	
	MainMenuButton startbutton;//开始按钮的索引
	MainMenuButton scorebutton;//积分榜按钮的索引
	MainMenuButton soundbutton;//声音控制按钮的索引
	
	float pointx;//屏幕上触控到的点
	float pointy;//屏幕上触控到的点
	
	Bitmap mainbackground;//主界面的背景图片
	Bitmap mainstartbutton;//开始按钮图片 
	Bitmap mainscroebutton;//查看积分按钮图片
	Bitmap soundoff;//关闭声音的按钮图片
	Bitmap soundon;//开启声音的按钮图片
	
	public MainView(MyActivity activity) {
		super(activity);
		this.activity=activity;
		getHolder().addCallback(this);//注册回调接口
	}

	@Override
	public void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		canvas.clipRect(new Rect(0,0,Constant.SCREEN_WIDTH,Constant.SCREEN_HEIGHT));//只在屏幕范围内绘制图片
		canvas.drawColor(Color.WHITE);//界面设置为白色
		//绘制主界面的背景图片
		canvas.drawBitmap(mainbackground, 0, 0, paint);
		//绘制开始按钮
		startbutton.drawSelf(canvas, paint);
		//绘制积分榜按钮
		scorebutton.drawSelf(canvas, paint);
		//绘制声音控制按钮
		soundbutton.drawSelf(canvas, paint);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		pointx=(float) event.getX();
		pointy=(float) event.getY();
		switch(event.getAction())
		{
		case MotionEvent.ACTION_DOWN://按下
			if(startbutton.isPointInRect(pointx, pointy))
			{//跳转到游戏界面
				activity.sendMessage(1);//发送消息，进入游戏界面
			}else if(scorebutton.isPointInRect(pointx, pointy))
			{
				//跳转到积分榜页面
				activity.sendMessage(3);

			}else if(soundbutton.isPointInRect(pointx, pointy))
			{
				soundbutton.setswitch();//转换图片
				//实现其相应功能
				activity.backgroundsoundFlag=soundbutton.isOnflag();//为背景音乐开启标志位赋值
			}
			break;
		case MotionEvent.ACTION_UP://抬起
			
			break;
		}
		return true;
	}
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		
		paint=new Paint();//创建画笔
		paint.setAntiAlias(true);//打开抗锯齿
		
		initBitmap();//初始化位图资源
		keythread=new KeyThread();//刷帧线程创建对象
		//开始按钮创建对象
		startbutton=new MainMenuButton(activity,mainstartbutton,mainstartbutton,5*Constant.SCREEN_WIDTH/16,5*Constant.SCREEN_HEIGHT/16);
		//积分按钮创建对象
		scorebutton=new MainMenuButton(activity,mainscroebutton,mainscroebutton,5*Constant.SCREEN_WIDTH/16,9*Constant.SCREEN_HEIGHT/16);
		//声音按钮创建对象
		soundbutton=new MainMenuButton(activity,soundon,soundoff,7*Constant.SCREEN_WIDTH/8,0);
		
		keythread.setKeyFlag(true);//刷帧线程的标志位设为true
		keythread.start();//开启刷帧线程
	}
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		keythread.setKeyFlag(false);//刷帧线程的标志位设为false
	}
	//初始化图片资源
	public void initBitmap(){
		//主界面的背景图片
		mainbackground=BitmapFactory.decodeResource(this.getResources(), R.drawable.main_background);
		mainbackground=PicLoadUtil.scaleToFit(mainbackground,Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);
		//开始按钮图片
		mainstartbutton=BitmapFactory.decodeResource(this.getResources(), R.drawable.startbutton);
		mainstartbutton=PicLoadUtil.scaleToFit(mainstartbutton,3*Constant.SCREEN_WIDTH/8, Constant.SCREEN_HEIGHT/8);
		//积分榜图片
		mainscroebutton=BitmapFactory.decodeResource(this.getResources(), R.drawable.scorebutton);
		mainscroebutton=PicLoadUtil.scaleToFit(mainscroebutton,3*Constant.SCREEN_WIDTH/8, Constant.SCREEN_HEIGHT/8);
		//开启声音按钮
		soundon=BitmapFactory.decodeResource(this.getResources(), R.drawable.sound_on);
		soundon=PicLoadUtil.scaleToFit(soundon,Constant.SCREEN_WIDTH/8, Constant.SCREEN_WIDTH/8);
		//关闭声音按钮
		soundoff=BitmapFactory.decodeResource(this.getResources(), R.drawable.sound_off);
		soundoff=PicLoadUtil.scaleToFit(soundoff,Constant.SCREEN_WIDTH/8, Constant.SCREEN_WIDTH/8);
	}
	
	//刷帧线程
	private class KeyThread extends Thread
	{
		private boolean keyFlag=false;
		@Override
		public void run()
		{
			while(isKeyFlag())
			{
				try
				{
					Thread.sleep(20);
				}catch(Exception e)
				{
					e.printStackTrace();
				}
				repaint();
			}
		}
		public void setKeyFlag(boolean keyFlag) {
			this.keyFlag = keyFlag;
		}
		public boolean isKeyFlag() {
			return keyFlag;
		}
	}
	
	//重新绘制画面
	@SuppressLint("WrongCall")
	public void repaint()
	{
		Canvas canvas=this.getHolder().lockCanvas();
		try
		{
			synchronized(canvas)
			{
				onDraw(canvas);
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			if(canvas!=null)
			{
				this.getHolder().unlockCanvasAndPost(canvas);
			}
		}
	}
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		
	}

}
